package com.oms.dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import com.liudecai.utils.jdbc.TxQueryRunner;
import com.oms.bean.FoodCategory;

public class FoodCategoryDao {
	QueryRunner qr=null;//查询运行器
	public FoodCategoryDao() {
		qr = new TxQueryRunner();
	}

	public void Add(FoodCategory foodcategory) throws SQLException {
		String sql = "insert into oms_food_category(category_id,category_name,add_time)values(?,?,?)";
		qr.update(sql, foodcategory.getCategory_id(),foodcategory.getCategory_name(),foodcategory.getAdd_time());
	}

	public void Delete(int id) throws SQLException {
		String sql = "delete from oms_food_category where id=?";
		qr.update(sql, id);
	}

	public void Update(FoodCategory foodcategory) throws SQLException {
		String sql="update oms_food_category set category_id=?,category_name=?,add_time=? where id=?";
		qr.update(sql,foodcategory.getCategory_id(),foodcategory.getCategory_name(),foodcategory.getAdd_time(),foodcategory.getId());
	}

	public FoodCategory FindById(int id) throws SQLException {
		String sql="select id, category_id, category_name, add_time from oms_food_category where id=?";
		FoodCategory foodCategory=qr.query(sql, new BeanHandler<FoodCategory>(FoodCategory.class),id);
		return foodCategory;
	}

	public List<FoodCategory> FindByString(String str) throws SQLException {
		String sql="select * from oms_food_category S where Concat(S.*) like '%"+str+"%'";
		List<FoodCategory> foodCategories=qr.query(sql, new BeanListHandler<FoodCategory>(FoodCategory.class));
		return (List<FoodCategory>) foodCategories;
	}

	public List<FoodCategory> FindAll() throws SQLException {
		String sql="select * from oms_food_category";
		List<FoodCategory> foodCategories=qr.query(sql, new BeanListHandler<FoodCategory>(FoodCategory.class));
		return foodCategories;
	}

	public long FoodCategoryCount() throws SQLException {
		// TODO Auto-generated method stub
		String sql="select count(id) from oms_food_category";
		return qr.query(sql, new ScalarHandler<Long>());
	}

}
